/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<TreeNode *> allPossibleFBT(int n)
    {
        return fbt(n);
    }

    vector<TreeNode *> fbt(int n)
    {
        if (n == 1)
        {
            return {new TreeNode()};
        }

        vector<TreeNode *> result{};
        for (int i = 1; i <= n - 2; i += 2)
        {
            auto lefts = fbt(i);
            auto rights = fbt(n - i - 1);
            for (auto left : lefts)
            {
                for (auto right : rights)
                {
                    result.push_back(new TreeNode(0, left, right));
                }
            }
        }
        return result;
    }
};